<template>
	<view class="footer-cart-buy">
		<view class="left">
			<span class="title">价格：</span>
			<span class="unit">¥</span>
			<span class="price">{{price}}</span>
		</view>
		<view class="right">
			<button class="btn add-cart" @click="onAddCartTap">加入购物车</button>
			<button class="btn pay-now" @click="onBuyNowTap">立即结算</button>
		</view>
	</view>
</template>

<script>
	import { requestErrorTip } from '@/common/tools.js';
	
	export default {
		name: 'footerCartBuy',
		props: {
			productId: {
				type: Number,
				default: ''
			},
			price: {
				type: String,
				default: ''
			},
			type: {
				type: Number,
				default: ''
			},
		},
		data() {
			return {
			}
		},
		methods: {
			onAddCartTap (cb) {
				this.$Requset({
					url: this.$Api['cart']['add'],
					data: {
						product_id: this.productId,
						cat_type: this.type,
						nums: 1,
						num_type: 2
					},
					success: (res) => {
						const { status, msg } = res;
						if(status && typeof cb === 'function') {
							cb(res);
						} else {
							requestErrorTip('toast', msg);
						}
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			// 立即购买
			onBuyNowTap () {
				this.onAddCartTap((res) => {
					uni.navigateTo({
						url: '/pages/order/create-order?ids=' + res.cart_id
					});
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 底部购物车
	.footer-cart-buy{
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		background-color: #FFFFFF;
		border-top: 1rpx solid #E4EAED;
		padding: 0 30rpx;
		.left{
			float: left;
			.title{
				font-size: 22rpx;
				color: #1A1A1A;
			}
			.unit{
				font-size: 24rpx;
				font-weight: bold;
				color: $color-common;
				margin-right: 4rpx;
			}
			.price{
				font-size: 40rpx;
				font-weight: bold;
				color: $color-common;
			}
		}
		.right{float: right;
			position: relative;
			height: 72rpx;
			font-size: 0;
			padding: 14rpx 0 14rpx 18rpx;
			&::before{
				content: '';
				position: absolute;
				top: 50%;
				left: 0;
				margin-top: -20rpx;
				height: 40rpx;
				width: 1rpx;
				background-color: #E4EAED;
			}
			.btn{
				display: inline-block;
				height: 72rpx;
				line-height: 72rpx;
				font-size: 26rpx;
				color: #FFF;
				text-align: center;
				padding: 0 45rpx;
				border-radius: 36rpx;
				margin-left: 12rpx;
			}
			.add-cart{
				background-color: #373737;
			}
			.pay-now{
				background-color: $color-common;
			}
		}
	}
</style>
